package com.example.poemtest.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.DynastryPoetryBean;

import java.util.List;

/**
 * created by JHT on 2024/4/2
 * 朝代的适配器
 */
public class PoetryDynastyAdapter extends BaseAdapter {

    private Context mContext;
    private List<DynastryPoetryBean.DynastyPoetry> poetryList;
    private int layoutResId;

    public PoetryDynastyAdapter(Context mContext, List<DynastryPoetryBean.DynastyPoetry> poetryList, int layoutResId) {
        this.mContext = mContext;
        this.poetryList = poetryList;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (poetryList != null) {
            return poetryList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (poetryList != null) {
            return poetryList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHolder;
        if (convertView == null) {
            //加载item的布局控件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取item中的控件对象
            viewHolder = new ViewHold();
            viewHolder.poetryContent = convertView.findViewById(R.id.poetry_content);
            viewHolder.poetryTitle = convertView.findViewById(R.id.poetry_title);
            viewHolder.poetryAuther= convertView.findViewById(R.id.poetry_auther);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        } else {
            //重复利用converView对象
            //获取到跟当前的converView绑定在一起的ViewHolder对象
            viewHolder = (ViewHold) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.poetryContent.setText(poetryList.get(position).getTag());
        viewHolder.poetryTitle.setText(poetryList.get(position).getTitle());
        viewHolder.poetryAuther.setText(poetryList.get(position).getAutuor());
        return convertView;
    }

    private static class ViewHold {
        private TextView poetryContent, poetryTitle, poetryAuther;
    }

}
